package com.dd.ent.car.ui.home.templet.video;


import android.widget.Toast;

import com.dd.ent.car.AppContext;
import com.dd.ent.car.common.bean.base.PageBean;
import com.dd.ent.car.common.bean.base.ResultBean;
import com.dd.ent.car.common.bean.video.Video;
import com.dd.ent.car.common.config.UIConfig;
import com.dd.ent.car.common.player.MediaPlayerManager;
import com.dd.ent.car.service.video.VideoService;
import com.dd.ent.car.ui.adapter.base.BaseListAdapter;
import com.dd.ent.car.ui.adapter.general.VideoAdapter;
import com.dd.ent.car.ui.home.templet.HomeTempletListFragment;
import com.google.gson.reflect.TypeToken;
import com.pp.ent.base.log.MLog;
import com.pp.ent.core.manage.UIHandler;
import com.pp.ent.view.annotation.Inject;

import java.lang.reflect.Type;

/**
 * 博客界面
 */
public class VideoFragment extends HomeTempletListFragment<Video> {

    public static final String BUNDLE_BLOG_TYPE = "BUNDLE_BLOG_TYPE";

    public static final String HISTORY_BLOG = "history_blog";
    private boolean isFirst = true;
    private final static String TAG = "VideoFragment";
    private int startPos = 1;
    @Inject
    private VideoService videoService;

    @Override
    protected void initData() {
        super.initData();
        requestData();
    }

    @Override
    public void onRefreshing() {
        isFirst = true;
        startPos = 1;
        super.onRefreshing();
    }

    @Override
    protected void requestData() {
        super.requestData();
        videoService.queryVideoByPage(startPos, ONE_PAGE_MAX_SIZE);

    }

    @Override
    protected BaseListAdapter<Video> getListAdapter() {
        return new VideoAdapter(this);
    }

    @Override
    protected Type getType() {
        return new TypeToken<ResultBean<PageBean<Video>>>() {
        }.getType();
    }

    @Override
    protected String getPlayTag() {
        return MediaPlayerManager.PlayerTag.FEED;
    }

    @Override
    public void onPause() {
        super.onPause();
        MediaPlayerManager.getInstance().releaseMediaPlayer(MediaPlayerManager.PlayerTag.FEED);
    }

    @Override
    public void onPageChanged(int number) {
        super.onPageChanged(number);
        MediaPlayerManager.getInstance().releaseMediaPlayer(MediaPlayerManager.PlayerTag.FEED);
    }

    @UIHandler(UIConfig.QUERY_ALL_FEED_VIDEO_SUCCESS)
    public void getListChocieRes(String json) {
        MLog.debug(TAG, "getListChocieRes json=" + json);
        ResultBean<PageBean<Video>> resultBean = AppContext.createGson().fromJson(json, getType());
        setListData(resultBean);
        onRequestFinish();

    }

    @UIHandler(UIConfig.ADD_VIDEO_INFO_TO_SERVER_SUCCESS)
    public void addVideoInfoServerResponse() {
        onRefreshing();
    }


    @Override
    protected void setListData(ResultBean<PageBean<Video>> resultBean) {
        super.setListData(resultBean);
        startPos = mAdapter.getCount() / ONE_PAGE_MAX_SIZE + 1;
    }

}
